<template>
  <div class="enterprise-list-wrap">
    <el-form ref="searchForm" :model="filters" label-width="80px" :inline="true">
      <el-form-item label="企业名称" prop="name">
        <el-input v-model="filters.name" size="medium" style="width: 250px" />
      </el-form-item>
      <el-button v-clickForbidden size="medium" type="primary" @click="searchEnterprise">搜索</el-button>
      <el-button v-clickForbidden size="medium" type="primary" plain @click="enterpriseRecord">申请记录</el-button>
      <el-button v-clickForbidden size="medium" type="primary" plain @click="enterpriseExamin">邀请记录</el-button>
    </el-form>
    <el-table
      class="table-list"
      :data="enterpriseList"
      stripe
      border
      style="width: 100%"
    >
      <el-table-column label="序号" type="index" width="50" align="center" />
      <el-table-column prop="name" :show-overflow-tooltip="true" label="企业名称" min-width="150" align="center" />
      <el-table-column prop="sysEnterpriseType" :formatter="formartType" label="企业类型" min-width="80" align="center" />
      <el-table-column prop="legalperson" label="单位法人" min-width="80" align="center" />
      <el-table-column prop="creditCodePic" label="信用编码" min-width="170" align="center" />
      <el-table-column label="操作" align="center" width="80">
        <template slot-scope="{row}">
          <i class="el-icon-circle-check select" @click="selectEnterprise(row.id)" />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <pagination
      class="pagination-container"
      :total="total"
      :page.sync="filters.pageNum"
      :limit.sync="filters.pageSize"
      @pagination="searchEnterprise"
    />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { enterpriseList } from '@/api/system/profile'
import { personalAddEnterprise } from '@/api/common/approvalReminder'
export default {
  name: 'EnterpriseMange',
  props: {
    tableData: {
      type: Array,
      default: () => []
    },
    typeOptions: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      total: 0,
      // 企业名称
      filters: {
        pageNum: 1,
        pageSize: 20,
        enterpriseType: 0,
        name: ''
      },
      // 企业列表
      enterpriseList: [],
      rules: {
        name: [{ required: true, message: '企业名称不能为空!', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapState({ userInfo: state => state.user.userInfo })
  },
  methods: {
    formartType(row) {
      if (JSON.stringify(row) === '{}') return
      const item = this.typeOptions.find(item => item.typeNum === row.sysEnterpriseType)
      return item && item.typeName
    },
    // 查询企业
    async searchEnterprise() {
      const { data: { rows, total }} = await enterpriseList(this.filters)
      this.enterpriseList = rows
      this.total = total
    },
    // 选择企业加入
    selectEnterprise(enterpriseId) {
      this.$confirm('是否加入该企业?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async() => {
        const data = { enterpriseId, accountType: 0, applyType: 0, userJson: JSON.stringify(this.userInfo) }
        await personalAddEnterprise(data)
        this.msgSuccess('等待企业审核！')
      }).catch(() => {})
    },
    // 申请企业记录
    enterpriseRecord() {
      this.$router.push('/profile/enterpriseRecord')
    },
    enterpriseExamin() {
      this.$router.push('/profile/enterpriseExamin')
    }
  }
}
</script>

<style lang='scss' scoped>
  .enterprise-list-wrap{
    margin-bottom: 20px;
  }
  .select{
    font-size: 18px;
    cursor: pointer;
    color:#0a7fff;
  }
</style>
